package com.apusic.common;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.github.pagehelper.PageHelper;
import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
//MapperScan(basePackages = "com.kingdee.apusic.admin.dao.*Dao", sqlSessionTemplateRef = "sqlSessionTemplate")
public class DatasourceC3P0Config {

	private static final Logger logger = LoggerFactory
			.getLogger(DatasourceC3P0Config.class);

	@Bean
	public PageHelper pageHelper() {
		logger.info("****************注册MyBatis分页插件PageHelper");
		PageHelper pageHelper = new PageHelper();
		Properties p = new Properties();
		p.setProperty("offsetAsPageNum", "true");
		p.setProperty("rowBoundsWithCount", "true");
		p.setProperty("reasonable", "true");
		pageHelper.setProperties(p);
		return pageHelper;
	}

	@Bean(name = "dataSource")
	@ConfigurationProperties(prefix = "c3p0")
	@Primary
	// 用@Primary区分主数据源
	public DataSource dataSource() {
		return DataSourceBuilder.create().type(ComboPooledDataSource.class)
				.build(); // 创建数据源
	}

	@Bean(name = "sqlSessionFactory")
	@Primary
	public SqlSessionFactory sqlSessionFactory(
			@Qualifier("dataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		// 配置分页插件
		Interceptor[] plugins = new Interceptor[] { pageHelper() };
		bean.setPlugins(plugins);
		bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
		// bean.setMapperLocations(new PathMatchingResourcePatternResolver()
		// .getResources("com.kingdee.apusic.data.*.mapper.*Mapper.xml"));
		bean.setMapperLocations(new PathMatchingResourcePatternResolver()
				.getResources("classpath:mybatis/mapper/*Mapper.xml"));

		return bean.getObject();
	}

	@Bean(name = "transactionManager")
	@Primary
	public DataSourceTransactionManager transactionManager(
			@Qualifier("dataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = "sqlSessionTemplate")
	@Primary
	public SqlSessionTemplate sqlSessionTemplate(
			@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory)
			throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

}
